<html>
<head><meta charset="utf-8"><title>Sharing code across trait implementations · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html">Sharing code across trait implementations</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="194592778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194592778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194592778">(Apr 19 2020 at 11:25)</a>:</h4>
<p>We have a trait with plenty of associated types, but several (well, 2) instances use most of the same choices for that type, plus a bunch of methods depending on that type are then implemented the same way. Is there a way to share that code, without having to repeat the entire rest of the trait signature? I couldn't find one, even specialization seems not enough due to the involved assoc types (but maybe I am misunderstanding). Does anyone here have an idea? See <a href="https://github.com/rust-lang/rust/issues/71129" title="https://github.com/rust-lang/rust/issues/71129">https://github.com/rust-lang/rust/issues/71129</a>.</p>



<a name="194592829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194592829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194592829">(Apr 19 2020 at 11:26)</a>:</h4>
<p>Can you not use assoc. type defaults?</p>



<a name="194592884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194592884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194592884">(Apr 19 2020 at 11:28)</a>:</h4>
<p>but then default method impls cannot use those, or can they? I admit I didnt try that.</p>



<a name="194592889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194592889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194592889">(Apr 19 2020 at 11:28)</a>:</h4>
<p>No</p>



<a name="194592895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194592895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194592895">(Apr 19 2020 at 11:28)</a>:</h4>
<p>I am reading <a href="https://github.com/rust-lang/rfcs/blob/master/text/1210-impl-specialization.md" title="https://github.com/rust-lang/rfcs/blob/master/text/1210-impl-specialization.md">https://github.com/rust-lang/rfcs/blob/master/text/1210-impl-specialization.md</a> and quite confused by the <code>add_assign</code> example, it seems to me that <code>default impl&lt;T: Clone, Rhs&gt; Add&lt;Rhs&gt; for T</code> should not typecheck because <code>Output</code> might be different from <code>Self</code>.</p>



<a name="194592905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194592905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194592905">(Apr 19 2020 at 11:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194592889" title="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194592889">said</a>:</p>
<blockquote>
<p>No</p>
</blockquote>
<p>then it doesnt help. the types themselves aren't annoying to repeat, but the methods depending on them are.</p>



<a name="194592967"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194592967" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194592967">(Apr 19 2020 at 11:30)</a>:</h4>
<p>hm, specialization as described there should actually be able to do this, but I am not sure if it is already sufficiently implemented.</p>



<a name="194592982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194592982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194592982">(Apr 19 2020 at 11:31)</a>:</h4>
<p>Specialization also doesn't allow normalizing specializable associated types</p>



<a name="194593063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593063">(Apr 19 2020 at 11:33)</a>:</h4>
<p>Here's some <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=dee3d9e025d86fbab6369e3bec7bd8cc" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=dee3d9e025d86fbab6369e3bec7bd8cc">code to play with</a></p>



<a name="194593185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593185">(Apr 19 2020 at 11:36)</a>:</h4>
<p>I'd want <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=ae4bb988ec211689df64534d17bb5e9c" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=ae4bb988ec211689df64534d17bb5e9c">something like this</a>, but it doesn't work. I am not sure why though since in the specialized impl we should be able to rely on the type being <code>i32</code>.</p>



<a name="194593239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593239">(Apr 19 2020 at 11:37)</a>:</h4>
<p><code>default impl</code> makes all items <code>default</code>, so you cannot assume what the associated type normalizes to</p>



<a name="194593295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593295">(Apr 19 2020 at 11:38)</a>:</h4>
<p>I don't think I fully understand what you're trying to do here – <code>other</code> is never provided an implementation, is it a default trait method in the real code?</p>



<a name="194593423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593423">(Apr 19 2020 at 11:42)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194593239" title="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194593239">said</a>:</p>
<blockquote>
<p><code>default impl</code> makes all items <code>default</code>, so you cannot assume what the associated type normalizes to</p>
</blockquote>
<p>ah... I want to make the items I give non-default though</p>



<a name="194593445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593445">(Apr 19 2020 at 11:42)</a>:</h4>
<p>so there is no way to say "this is a partial impl but some items are final"? that seems like a big gap.</p>



<a name="194593453"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593453" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593453">(Apr 19 2020 at 11:42)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194593295" title="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194593295">said</a>:</p>
<blockquote>
<p>I don't think I fully understand what you're trying to do here – <code>other</code> is never provided an implementation, is it a default trait method in the real code?</p>
</blockquote>
<p>this is partial code. imagine <code>other</code> is implemented widely differently in the final implementations.</p>



<a name="194593456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593456">(Apr 19 2020 at 11:42)</a>:</h4>
<p>default trait methods also exist. there's around a dozen "other" methods.</p>



<a name="194593465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593465">(Apr 19 2020 at 11:43)</a>:</h4>
<p>(<a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/interpret/trait.Machine.html" title="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_mir/interpret/trait.Machine.html">this is the trait I am talking about</a>, but most of what it does concretely does not matter)</p>



<a name="194593524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593524">(Apr 19 2020 at 11:45)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194593295" title="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194593295">said</a>:</p>
<blockquote>
<p>I don't think I fully understand what you're trying to do here – <code>other</code> is never provided an implementation, is it a default trait method in the real code?</p>
</blockquote>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=304bd81ee0e24ae99c21d887f3b07033" title="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=304bd81ee0e24ae99c21d887f3b07033">here's some more code</a> if that helps</p>



<a name="194593658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593658">(Apr 19 2020 at 11:49)</a>:</h4>
<p>Hmm, yeah, not sure how to do this nicely...</p>



<a name="194593732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593732">(Apr 19 2020 at 11:51)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194593445" title="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194593445">said</a>:</p>
<blockquote>
<p>so there is no way to say "this is a partial impl but some items are final"? that seems like a big gap.</p>
</blockquote>
<p>this seems like a serious limitation. is that tracked somewhere? would the rfc1210 tracking issue be a good place to bring this up?</p>



<a name="194593805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194593805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194593805">(Apr 19 2020 at 11:52)</a>:</h4>
<p>Maybe, yeah</p>



<a name="194594207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594207">(Apr 19 2020 at 12:03)</a>:</h4>
<p>I only skimmed the earlier discussion but AFAICT nobody has stated the obvious yet: a low-tech way to factor out duplication in trait impls that can't be quite abstracted over with the trait system is to have macros generate the impls. This is commonly done for numeric types' impls. It has downsides and may not be worth it for just two impls, but maybe give it a shot.</p>



<a name="194594352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594352">(Apr 19 2020 at 12:07)</a>:</h4>
<p>ah yes... I tend to not even consider macros for abstraction/code sharing so I forgot about that approach (same recently happened with floating-point tests, where libstd also has a bunch of them and they use macros^^ I'll likely copy those tests to miri as well soon-ish)</p>



<a name="194594402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594402">(Apr 19 2020 at 12:08)</a>:</h4>
<p>With the intermediate trait, wouldn't you only need to write a single forwarding impl?</p>



<a name="194594415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594415">(Apr 19 2020 at 12:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194594402" title="#narrow/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations/near/194594402">said</a>:</p>
<blockquote>
<p>With the intermediate trait, wouldn't you only need to write a single forwarding impl?</p>
</blockquote>
<p>the intermediate trait has to repeat all forwarded signatures though</p>



<a name="194594436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594436">(Apr 19 2020 at 12:09)</a>:</h4>
<p>in this case, that kills any wins of code sharing we might get elsewhere</p>



<a name="194594438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594438">(Apr 19 2020 at 12:09)</a>:</h4>
<p>Yes. Only once for each method though, and then all users don't have to deal with it.</p>



<a name="194594478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594478">(Apr 19 2020 at 12:10)</a>:</h4>
<p>there's 2 users <span aria-label="rofl" class="emoji emoji-1f923" role="img" title="rofl">:rofl:</span></p>



<a name="194594482"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594482" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594482">(Apr 19 2020 at 12:10)</a>:</h4>
<p>Yeah, I guess it doesn't matter much which workaround you use here :D</p>



<a name="194594484"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594484" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594484">(Apr 19 2020 at 12:10)</a>:</h4>
<p>well a base requirement is that it has to actually save LOC ;)</p>



<a name="194594487"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594487" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594487">(Apr 19 2020 at 12:11)</a>:</h4>
<p>and the intermediate trait doesn't do that</p>



<a name="194594507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594507">(Apr 19 2020 at 12:11)</a>:</h4>
<p>but the macro does?</p>



<a name="194594509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594509">(Apr 19 2020 at 12:11)</a>:</h4>
<p>I think it would, yes</p>



<a name="194594512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594512">(Apr 19 2020 at 12:11)</a>:</h4>
<p>Well, it only has one copy of each common signature</p>



<a name="194594549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594549">(Apr 19 2020 at 12:12)</a>:</h4>
<p>"it"?</p>



<a name="194594550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594550">(Apr 19 2020 at 12:12)</a>:</h4>
<p>The macro solution</p>



<a name="194594553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594553">(Apr 19 2020 at 12:12)</a>:</h4>
<p>more importantly, it has 0 copies of "forwarded" method's signatures (the ones that are not shared)</p>



<a name="194594584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594584">(Apr 19 2020 at 12:13)</a>:</h4>
<p>that is what killed the intermediate trait</p>



<a name="194594646"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594646" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594646">(Apr 19 2020 at 12:14)</a>:</h4>
<p>so from all solutions so far that actually compile, I like the macro most :D</p>



<a name="194594651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594651">(Apr 19 2020 at 12:14)</a>:</h4>
<p>specialization would be neat but the current implementation (even the current RFC) seem too limited for that</p>



<a name="194594792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594792">(Apr 19 2020 at 12:18)</a>:</h4>
<p>Honestly, with only two similar-ish implementations which serve different purposes, I am skeptical if merging the common code is a good idea at all. The macro approach saves LOC, but it creates extra work whenever something stops being shared by the two impls and gets less attractive the more they diverge. I don't know this code very well, but it seems both CTFE and ConstProp are only going to become more elaborate in the future, and probably in different ways.</p>



<a name="194594816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194594816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194594816">(Apr 19 2020 at 12:19)</a>:</h4>
<p>If the duplication of signatures is the real thing bothering you, I'm almost tempted to suggest that the macro (if any) should focus on <em>those</em>, not supply any method bodies, and thus be applicable to all impls of <code>Machine</code>. But even this would probably be considered an unwise abuse of macros.</p>



<a name="194596870"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194596870" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194596870">(Apr 19 2020 at 13:08)</a>:</h4>
<p>so far, of the things I plan to share, I find it really unlikely that anything in there will stop being shared between the two impls</p>



<a name="194596883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194596883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194596883">(Apr 19 2020 at 13:08)</a>:</h4>
<p>in particular I dont think we'll ever use stacked-borrows-style ptr tagging in either of them</p>



<a name="194676014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Sharing%20code%20across%20trait%20implementations/near/194676014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Sharing.20code.20across.20trait.20implementations.html#194676014">(Apr 20 2020 at 13:41)</a>:</h4>
<p><a href="https://github.com/hobofan/ambassador" title="https://github.com/hobofan/ambassador">https://github.com/hobofan/ambassador</a><br>
There's this crate that maybe helps and maybe not.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>